<template>
    <div>
        <h2>医院设置列表</h2>
        <el-form :inline="true" class="demo-form-inline">
            <el-form-item>
                <el-input v-model="searObj.hosname" placeholder="医院名称"></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="searObj.hoscode" placeholder="医院编号"></el-input>
            </el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="getlist()"></el-button>
        </el-form>
        <div>
            <el-button type="danger" size="mini" @click="removeRows()">批量删除</el-button>
        </div>
        <el-table :data="list" style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column type="index" label="序号" width="50"></el-table-column>
            <el-table-column prop="hosname" label="医院名称"></el-table-column>
            <el-table-column prop="hoscode" label="医院编号"></el-table-column>
            <el-table-column prop="apiUrl" label="api基础路径"></el-table-column>
            <el-table-column prop="contactsName" label="联系人姓名"></el-table-column>
            <el-table-column prop="contactsPhone" label="联系人手机"></el-table-column>
            <el-table-column label="状态" width="80">
                <template slot-scope="scope">
                    {{ scope.row.status === 1 ? '可用' : '不可用' }}
                </template>
            </el-table-column>

            <el-table-column label="操作" width="280" align="center">
                <template slot-scope="scope">
                    <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除
                    </el-button>
                    <el-button v-if="scope.row.status === 1" type="primary" size="mini" icon="el-icon-delete"
                        @click="lockHospSet(scope.row.id, 0)">锁定</el-button>
                    <el-button v-if="scope.row.status === 0" type="danger" size="mini" icon="el-icon-delete"
                        @click="lockHospSet(scope.row.id, 1)">解除锁定</el-button>
                    <router-link :to="'/hospSet/edit/' + scope.row.id">
                        <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
                    </router-link>
                </template>
            </el-table-column>

        </el-table>
        <!--分页-->
        <el-pagination :current-page="current" :page-size="limit" :total="total"
            style="padding:30px 0 ; text-align: center; " layout="total,prev,pager,next,jumper"
            @current-change="getlist" />
    </div>
</template>

<script>
//引入接口定义的文件
import hospset from '@/api/hospset'

export default ({
    data() {
        return {
            current: 1, //当前页
            limit: 3,//每页现实记录数
            searObj: {},//条件封装对象
            list: [],//每页数据集合
            total: 0,
            mutipleSelection: []
        }
    },
    created() {//在页面渲染之前执行
        this.getlist()
    },
    methods: {//定义方法请求接口

        //医院设置列表
        getlist(page = 1) {
            this.current = page
            hospset.getHospSetList(this.current, this.limit, this.searObj)
                .then(response => {
                    //放回集合赋值给list
                    this.list = response.data.records
                    this.total = response.data.total
                })//请求成功
                .catch(error => {
                    console.log(error)
                })//请求失败
        },
        //删除医院设置
        removeDataById(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                hospset.deleteHpspSet(id)
                    .then(response => {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        })
                        this.getlist(1)
                    })
            })
        },
        //获取选择复选框的id
        handleSelectionChange(selection) {
            this.mutipleSelection = selection
        },
        //锁定和取消锁定
        removeRows() {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var ldlist = []
                for (var i = 0; i < this.mutipleSelection.length; i++) {
                    var obj = this.mutipleSelection[i]
                    var id = obj.id
                    ldlist.push(id)
                }
                hospset.batchHospSet(ldlist)
                    .then(response => {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        })
                        this.getlist(1)
                    })
            })
        },
        lockHospSet(id, status) {
            hospset.lockHospSet(id, status)
                .then(response => {
                    this.getlist()
                })

        },

    },
})
</script>
